var app = getApp()
import util from '../../utils/utils';

// //////////////////////////////////////////////////
var arrayincp = [] //待办
var arraycp = [] //已办
var array //待办
var array1 //已办


// ////////////////////////////////////////////////

Page({

  data: {

    pictureUrl: "",
    p2_toptexts: "",



    tabs: [{
        id: 0,
        name: "日记",
        isActive: true
      },
      {
        id: 1,
        name: "待办",
        isActive: false
      }
    ],

    dairyList: [],

    hasBind: false,
    lastDairyId: 1,
    hasBindForDel: false,
    dairyid: 0,
    //刷新页面
    hasRe: false,

    // /////////////////////////////////////////////////////
    array: arrayincp,
    array1: arraycp,
    condition1: true,
    condition2: false,
    input: false,
    nav1: "nav1",
    nav2: "nav2"

    // ////////////////////////////////////////////////////

  },


  not_delPost:function(){
    this.setData({
      hasBindForDel: false
    })
  },
  //实现长按删除
  deletePost: function (e) {
    var ccc = e.currentTarget.dataset.id; //获取view中的药用currentTarget
    this.setData({
      hasBindForDel: true,
      dairyid: ccc
    })
  },
  item1: function () {
    this.setData({
      hasBindForDel: false
    })
  },
  item2: function (e) {
    this.setData({
      hasBindForDel: false
    });
    //数据库找到id删除
    wx.request({
      url: 'http://120.24.97.106:8081/delDairyById',
      method: 'POST',
      data: {
        "id": this.data.dairyid,
      },
      success: (result) => {
        wx.showLoading({
          title: '保存中',
        })
        //用onLoad周期方法重新加载，实现当前页面的刷新
        this.onLoad();
        setTimeout(function () {
          wx.switchTab({
            url: '../p2/p2',
          });
          wx.hideLoading()
        }, 500)
      }
    });

  },



  hanldeItemTap: function (e) {
    const {
      index
    } = e.currentTarget.dataset;
    let {
      tabs
    } = this.data;
    tabs.forEach((v, i) => i == index ? v.isActive = true : v.isActive = false);
    this.setData({
      tabs
    })
  },

  dairyAdd: function () {
    this.setData({
      hasBind: true
    })
  },
  notDairyAdd: function () {
    this.setData({
      hasBind: false
    })
  },

  bindFormSubmit: function (e) {
    let {
      textarea
    } = e.detail.value;



    //获取时间
    var time = util.formatTime(new Date());
    wx.request({
      url: 'http://120.24.97.106:8081/saveDairy',
      method: 'POST',
      data: {
        "id": this.data.lastDairyId,
        "userid": app.userid,
        "releasetime": time,
        "texts": textarea
      },
      success: (result) => {
        wx.showLoading({
          title: '保存中',
        })
        //用onLoad周期方法重新加载，实现当前页面的刷新
        this.onLoad();
        setTimeout(function () {
          // wx.switchTab({
          //   url: '../p2/p2',
          // });
          wx.hideLoading()
        }, 500)
      }
    });



    this.setData({
      hasBind: false,
    })


  },

  onLoad: function (options) {



    wx.request({
      url: 'http://120.24.97.106:8081/findMyDairy',
      method: 'POST',
      data: {
        "userid": app.userid,
      },
      success: (result) => {
        this.setData({
          dairyList: result.data
        })

      }
    })

    //获得dairys表中最后一行的id,然后加1
    wx.request({
      url: 'http://120.24.97.106:8081/getDairyLastId',
      method: 'GET',
      success: (result) => {
        const x = result.data + 1
        this.setData({
          lastDairyId: x
        })
      }
    })

    this.setData({
      pictureUrl: app.pictureUrl,
      p2_toptexts: app.p2_toptexts
    })


    // //////////////////////////////////////////////////////

    // var that = this
    wx.getStorage({
      key: 'arrayList',
      success: (res) => {
        var arraystore = res.data
        // console.log(arraystore)
        arrayincp = arraystore[0]
        arraycp = arraystore[1]
        this.setData({
          array: arrayincp,
          array1: arraycp,
        })
      }
    })

    // var array2 = wx.getStorageSync('array')
    // console.log(array2)



    // //////////////////////////////////////////////////////


  },


  // //////////////////////////////////////////////////////////
  //点击待办
  click1: function (e) {

    this.setData({
      condition1: true,
      condition2: false,
      nav1: "nav1",
      nav2: "nav2",
      input: false
    })
  },

  //点击已办
  click2: function (e) {

    this.setData({
      condition1: false,
      condition2: true,
      nav1: "nav2",
      nav2: "nav1",
      input: false
    })
  },

  //待办变已办
  short: function (e) {
    var id = e.target.id
    var newitem = arrayincp[id]
    arrayincp.splice(id, 1)
    arraycp.push(newitem)
    this.setData({
      array: arrayincp,
      array1: arraycp,
    })

    var arraystore = [arrayincp, arraycp]

    // console.log(arraystore)
    wx.setStorage({
      key: "arrayList",
      data: arraystore
    })
  },
  //增加
  click: function (e) {
    this.setData({
      input: true,
      condition1: false,
      condition2: false,
      nav1: "nav2",
      nav2: "nav2",
    })

  },
  // 输入完成
  confirm: function (e) {
    arrayincp.push(e.detail.value)
    this.setData({
      array: arrayincp,
      input: false,
      condition1: true,
      condition2: false,
      nav1: "nav1",
      nav2: "nav2",
    })

    // var arraystore = [arrayincp, arraycp]
    var arraystore = [arrayincp, arraycp]

    // console.log(arraystore)
    wx.setStorage({
      key: "arrayList",
      data: arraystore
    })

  },

  //卸载页面，储存数据
  onUnload: function () {
    // var arraystore = [arrayincp, arraycp]
    // console.log(arraystore)
    // wx.setStorage({
    //   key: "arrayList",
    //   data: arraystore
    // })
  },



  // /////////////////////////////////////////////////////////


  onShow: function () {
    if (app.p2_shuaxin) {
      app.p2_shuaxin=false
      //刷新页面
      this.onLoad()
    }

  },

})